package com.link.base.base.invtxn.controller;

import com.link.base.base.invtxn.model.Invtxn;
import com.link.base.base.invtxn.service.InvtxnService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @desc 交易信息处理
 */
@Controller
@RequestMapping("/link/invtxn")
public class InvtxnController extends BasicController<Invtxn> {

    //private static final Logger logger = LogManager.getLogger(PortalInvtxnController.class);

    @Resource
    private InvtxnService invtxnService;

    @Override
    public BasicService<Invtxn> getBasicService() throws Exception {
        return invtxnService;
    }

    @RequestMapping(value = "/goodConfirm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> goodConfirm(Long id, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (null == id) {
                throw new ServiceException("PUBLIC-007");
            }
            invtxnService.goodConfirm(id);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>查询到货记录</p>
     *
     * @author guoshan
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    /**合并同一天内同一件产品到货数量，返回此订单到货记录*/
    @RequestMapping(value = "/queryArrivedHisPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> arrivedHis(@JsonParam QueryParams qps, @JsonParam Invtxn record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        Long orderId = record.getOrderId();
        try {
            if (null == orderId) {
                result.put("success", false);
                result.put("message", "订单Id不能为空！");
                return result;
            }
            Invtxn invtxn = (Invtxn) BasicModel.transformClass(Invtxn.class, qps);
            List<Invtxn> invtxns = invtxnService.queryByExamplePage(invtxn);
            List<Invtxn> list = new ArrayList<>();

            for (Invtxn invtxn2 : invtxns) {
                if (null == invtxn2.getProdId() || StringUtils.isBlank(invtxn2.getTradeDate())) {
                    break;
                }
                if (list.isEmpty()) {
                    list.add(invtxn2);
                } else {
                    boolean flag = true;
                    for (int i = 0; i < list.size(); i++) {
                        Invtxn invtxn3 = list.get(i);
                        if (invtxn2.getTradeDate().equals(invtxn3.getTradeDate())
                                && invtxn2.getProdId().equals(invtxn3.getProdId())) {
                            invtxn3.setAmount(invtxn2.getAmount() + invtxn3.getAmount());
                            flag = false;
                            break;
                        }

                    }
                    if (flag) {
                        list.add(invtxn2);
                    }
                }
            }
            result.put("rows", list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
